module.exports = (req, res, next) => {
    const jwt = require('jsonwebtoken')
    const { secret } = require('../config/config')

    let token = req.headers.authorization
    //切割token的bearer，拿到真正的token
    if (!token) {
        return res.json({ code: 1, message: "身份认证失败" })
    }
    token = token.split(' ')[1]
    jwt.verify(token, secret, (err, data) => {
        if (err) {
            console.log(token);
            return res.json({
                code: 1,
                msg: 'token错误',
                data: null
            })
        }
        req.decoded = data
        //token校验成功
        next()
    })
}